<?
include_once("/home/gru/public_html_dev/lib/common.php");
include_once("/home/gru/public_html_dev/lib/cDB.php");
include_once("/home/gru/public_html_dev/lib/cDAO.php");

function cropImg($imgURL,$w,$h,$f_er,$name){
	$today = date("Ymd");
	$dir = '/home/gru/img/'.$f_er.'/'.$today.'/';
	$file= '/home/gru/img/'.$f_er.'/'.$name;
		
	if(!file_exists($dir)){
		if(!mkdir($dir)){
			echo "error mkdir get.php";
			return 0;
		}
	}
  
  	if($imgURL=="")return;	
	if(is_file($file))return;
	
	
	debug_flush($imgURL);
	debug_flush($file);
	
	try{		
	  $im = new Imagick();
	  $im->readImage($imgURL);
	  $im->cropThumbnailImage ( $w, $h );
	  
	  $im->writeImage($file);
	  $im->destroy();
	  
	  return true;
	}catch(Exception $e){
	  return $e->getMessage()."<br>";
		
	}
}

$db= new cDB();
$dao = new cDAO($db->getCon());

//-----------------------------------------------------------------------------
// 섬네일 업데이트
//-----------------------------------------------------------------------------
$opt = "";
//$opt = " where id > 7000";
$opt = " where mImgS = '0' limit 200";
$q  = " SELECT id,mimg,mImgS,pid,domain FROM coupon $opt";

$arr = $dao->getSheet($q);
if($arr==NULL)exit;


foreach($arr as $i=>$v){
	$id		= $v['id'];
	$qu = " UPDATE coupon SET mImgS = '1' WHERE id = $id";
	$dao->query($qu);
}

foreach($arr as $i=>$v){
	$id		= $v['id'];
	$mimg	= $v['mimg'];
	$mImgS	= $v['mImgS'];
	$pid	= $v['pid'];
	$domain	= $v['domain'];
	
	$today = date("Ymd");
	$file = str_replace("/","_",$domain."_".$pid).".jpg";
	$mImgS = $today.'/'.$file;//섬네일경로
	
	//섬네일 생성
	if(cropImg($mimg,300,160,"s",$mImgS)){
		$qu = " UPDATE coupon SET mImgS = '$mImgS' WHERE id = $id";
		echo $qu;
		$dao->query($qu);
	}
}

$db->close();
?>